//
//  FindPsdVC.swift
//  LeGou
//
//  Created by lxm on 2022/7/26.
//

import UIKit

class FindPsdVC: ZMViewController {

    //MARK:--------------------属性--------------------
    var phoneStr = ""
    
    //0 找回密码 1修改密码
    var type = 0
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var phone: UILabel!
    
    @IBOutlet weak var codeText: UITextField!
    
    @IBOutlet weak var psd: UITextField!
    
    @IBOutlet weak var ensurePsd: UITextField!
    
    @IBOutlet weak var confirmBtn: UIButton!
    
    @IBOutlet weak var titleLabel: UILabel!
    
    
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        title = "找回密码"
        phone.text = phoneStr.zm.hidPhone()
        codeText.delegate = InputMananger.shard.onlyNumber
        if type == 0 {
            // 找回密码
            titleLabel.text = "找回密码"
            psd.placeholder = "密码至少为8位数字字母组合"
        } else {
            // 修改密码
            titleLabel.text = "修改密码"
            psd.placeholder = "请设置您的新密码"
        }
    }
    
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        let psd = psd.rx.text.orEmpty.map({$0.zm.match(CONFIG_PSD_REGULAR)})
        let psdEnsure = ensurePsd.rx.text.orEmpty.map({$0.zm.match(CONFIG_PSD_REGULAR)})
        let code = codeText.rx.text.orEmpty.map({$0.count == 6})
        
        Observable.combineLatest([psd, psdEnsure, code]).map { oks ->Bool in
            for ok in oks {
                if !ok {return false}
            }
            return true
        }.subscribe(onNext:{ [unowned self] resp in
            self.confirmBtn.isEnabled = resp
            self.confirmBtn.backgroundColor = resp ? Style.mainColor : Style.disableColor
        }).disposed(by: disposeBag)
    }
    
    @IBAction func onEye(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        psd.isSecureTextEntry = !sender.isSelected
        ensurePsd.isSecureTextEntry = !sender.isSelected

    }
    
    
    @IBAction func onSendCode(_ sender: CountButton) {
        LoginAPI.sendCode(["phone" : phoneStr]).rxRequestNullAble(type: Any.self).subscribe(onNext:{ [unowned self] resp in
            sender.start(time: 60)
        }).disposed(by: disposeBag)
    }
    
    
    @IBAction func onConfirm(_ sender: Any) {
        if psd.text! != ensurePsd.text! {
            return ZMToast.showToast(title: "两次输入密码不一致")
        }
        ZMToast.showActivity()
        LoginAPI.psdReset(["captcha" : codeText.text!, "password" : psd.text!, "phone" : phoneStr]).rxRequestNullAble(type: Any.self).subscribe(onNext:{ [unowned self] resp in
            ZMToast.showToast(title: "成功")
            self.navigationController?.popViewController(animated: true)
        }).disposed(by: disposeBag)
    }

    //MARK:--------------------delegate--------------------

}
